package com.Example.Model.User;

import com.protos.Peer.Event;
import com.websocket.Observer;

/**
 * 测试lombok与InnerBuilder
 * @author zyf
 */

public class User implements Observer {
    private String name;
    private Event message;

    public User(String name) {
        this.name = name;
    }

    private User(String name, Event message) {
        this.name = name;
        this.message = message;
    }

    private User(Builder builder) {
        name = builder.name;
        message = builder.message;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     *
     * @param message 订阅到的消息
     */
    @Override
    public void notify(Event message) {
        this.message = message;
        if (message.hasBlk()) {
            readTransNum();
        }
    }

    public void readTransNum() {
        System.out.println(name + " Block内Transactions的条数： " +  message.getBlk().getTransactionsList().size());
    }

    /**
     * {@code User} builder static inner class.
     */
    public static final class Builder {
        private String name;
        private Event message;

        private Builder() {
        }

        /**
         * Sets the {@code name} and returns a reference to this Builder so that the methods can be chained together.
         *
         * @param name the {@code name} to set
         * @return a reference to this Builder
         */
        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        /**
         * Sets the {@code message} and returns a reference to this Builder so that the methods can be chained together.
         *
         * @param message the {@code message} to set
         * @return a reference to this Builder
         */
        public Builder setMessage(Event message) {
            this.message = message;
            return this;
        }

        /**
         * Returns a {@code User} built from the parameters previously set.
         *
         * @return a {@code User} built with parameters of this {@code User.Builder}
         */
        public User build() {
            return new User(this);
        }
    }
}
